/**
 * 
 */
package com.iwords.service.persistence.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

import com.iwords.service.object.SysLibEntity;
import com.iwords.service.persistence.SysLibPersistence;

/**
 * @author       BOLIN.HBC 
 * @time         2011-3-12    
 * @description  SysLib-PersistenceImpl       
 */
@Stateless
public class SysLibPersistenceImpl implements SysLibPersistence{
	@PersistenceContext(unitName = "iwords")
	private EntityManager em;

	protected EntityManager getEntityManager() {
		return em;
	}

	public void create(SysLibEntity entity) {
		getEntityManager().persist(entity);
	}

	public void edit(SysLibEntity entity) {
		getEntityManager().merge(entity);
	}

	public void remove(SysLibEntity entity) {
		getEntityManager().remove(getEntityManager().merge(entity));
	}

	public SysLibEntity find(Long id) {
		return getEntityManager().find(SysLibEntity.class, id);
	}

 

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<SysLibEntity> findAll() {
		CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
		cq.select(cq.from(SysLibEntity.class));
		return getEntityManager().createQuery(cq).getResultList();
	}

	

	public int count() {
		CriteriaQuery<Object> cq = getEntityManager().getCriteriaBuilder().createQuery();
		javax.persistence.criteria.Root<SysLibEntity> rt = cq.from(SysLibEntity.class);
		cq.select(getEntityManager().getCriteriaBuilder().count(rt));
		javax.persistence.Query q = getEntityManager().createQuery(cq);
		return ((Long) q.getSingleResult()).intValue();
	}

	 
	@SuppressWarnings("unchecked")
	public List<SysLibEntity> findSysLibs() {
		Query query = getEntityManager().createQuery("SELECT s from SysLibEntity s");
		try {
			return (List<SysLibEntity>) query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

 
	@SuppressWarnings("unchecked")
	public List<SysLibEntity> findRootSysLibs() {
		Query query = getEntityManager().createQuery("SELECT s from SysLibEntity s where s.pid=0");
		try {
			return (List<SysLibEntity>) query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<SysLibEntity> getChildrenList(Long libid) {
		Query query = getEntityManager().createQuery("SELECT s from SysLibEntity s where s.pid=?1");
		query.setParameter(1, libid);
		try {
			return (List<SysLibEntity>) query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	public SysLibEntity getLibByLibName(String libName) {
		Query query = getEntityManager().createQuery("SELECT s from SysLibEntity s where s.libname=?1");
		query.setParameter(1, libName);
		try {
			return (SysLibEntity) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

 

}
